import os
import requests
from bs4 import BeautifulSoup
import csv
from urllib.parse import urljoin  # 用于补全链接

# 设置请求头（模拟浏览器）
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
    "Referer": "https://movie.douban.com/",
}

# 获取网页内容
url = "https://movie.douban.com/top250"
try:
    response = requests.get(url, headers=headers, timeout=10)
    response.encoding = 'utf-8'
    response.raise_for_status()  # 检查请求是否成功
except requests.exceptions.RequestException as e:
    print(f"请求失败: {e}")
    exit()

# 解析数据
soup = BeautifulSoup(response.text, 'html.parser')
movie_list = soup.find('ol', class_='grid_view').find_all('li')
movies_data = []
base_url = "https://movie.douban.com"  # 用于补全链接

for movie in movie_list:
    try:
        title = movie.find('span', class_='title').get_text(strip=True)
        rating_num = movie.find('span', class_='rating_num').get_text(strip=True)
        comment = movie.find_all('span')[-1].get_text(strip=True)  # 短评
        directors_actors = movie.find('p').get_text(" ", strip=True).replace('\xa0', ' ')  # 合并文本并替换特殊空格
        relative_link = movie.find('a')['href']
        link = urljoin(base_url, relative_link)  # 补全完整链接
        pic = movie.find('img')['src']
        movies_data.append([title, rating_num, comment, directors_actors, link, pic])
    except Exception as e:
        print(f"解析电影数据时出错: {e}")
        continue  # 跳过当前电影，继续下一个


# 保存为 CSV 文件（自动选择可写路径）
def save_to_csv(data, filename):
    """尝试在桌面或当前目录保存 CSV 文件"""
    save_paths = [
        os.path.join(os.path.expanduser("~"), "Desktop", filename),  # 桌面路径
        os.path.join(os.getcwd(), filename)  # 当前目录
    ]

    for path in save_paths:
        try:
            with open(path, 'w', encoding='utf-8-sig', newline='') as f:
                writer = csv.writer(f)
                writer.writerow(["标题", "评分", "短评", "导演/主演", "详情链接", "海报链接"])
                writer.writerows(data)
            print(f"数据已保存到: {path}")
            return True
        except PermissionError:
            continue
    print("错误：所有路径均无写入权限！")
    return False


# 执行保存
save_to_csv(movies_data, "douban_movies.csv")

# 打印前3行数据验证
print("\n示例数据：")
for row in movies_data[:3]:
    print(row)